﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExampleConsole
{

    #region 观察者模型设计
    /// <summary>
    /// 观察者抽象类
    /// </summary>
    public abstract class Observer
    {
        public abstract void Update();
    }

    /// <summary>
    /// 主题
    /// </summary>
    public abstract class Subject
    {
        private IList<Observer> observers = new List<Observer>();

        public void Add(Observer obj)
        {
            observers.Add(obj);
        }

        public void Remove(Observer obj)
        {
            observers.Remove(obj);
        }
        /// <summary>
        /// 通知
        /// </summary>
        public void Notify()
        {
            foreach (Observer o in observers)
            {
                o.Update();
            }
        }
    }
    /// <summary>
    /// 具体的主题
    /// </summary>
    public class ConcreteSubject : Subject
    {
        private string state;

        public string State
        {
            get { return state; }
            set { state = value; }
        }
    }


    /// <summary>
    /// 具体的观察者
    /// </summary>
    public class ConcreteObserver : Observer
    {
        private string name;
        private string state;

        private ConcreteSubject subject;

        public ConcreteSubject Subject
        {
            get { return subject; }
            set { subject = value; }
        }

        public ConcreteObserver(ConcreteSubject subject, string name)
        {
            this.subject = subject;
            this.name = name;
        }

        public override void Update()
        {
            this.state = subject.State;
            Console.WriteLine(string.Format("{0}更新,状态{1}", this.name, this.state));
        }
    }
    #endregion

    #region 股票示例

    /*假设有一股票开盘价格16.50元，自从上市以来价格是不断下降，而且以1.00元的速度下降。
在股票降到12.00元时，股民灵动生活买入了股票。
在股票降到8.05元时，股民Jane买了股票。
     */

    /// <summary>
    /// 股票
    /// </summary>
    public abstract class Stock
    {
        private IList<Observer> observers = new List<Observer>();

        public void Add(Observer obj)
        {
            observers.Add(obj);
        }

        public void Remove(Observer obj)
        {
            observers.Remove(obj);
        }
        /// <summary>
        /// 通知
        /// </summary>
        public void Notify()
        {
            foreach (Observer o in observers)
            {
                o.Update();
            }
        }
    }

    /// <summary>
    /// 具体的股票
    /// </summary>
    public class ConcreteStock : Stock
    {
        private decimal price;

        public decimal Price
        {
            get { return price; }
            set { price = value; }
        }
    }

    /// <summary>
    /// 投资者
    /// </summary>
    public class ConcreteInvestor : Observer
    {
        private string name;

        private decimal price;

        private bool hasBuy = false;

        private ConcreteStock stock;

        public ConcreteStock Stock
        {
            get { return stock; }
            set { stock = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stock"></param>
        /// <param name="name"></param>
        /// <param name="price">期望价格</param>
        public ConcreteInvestor(ConcreteStock stock, string name, decimal price)
        {
            this.stock = stock;
            this.price = price;
            this.name = name;
        }

        public override void Update()
        {
            if (this.stock.Price <= price && !hasBuy)
            {
                Console.WriteLine(string.Format("{0}买进了{1}元的股票", this.name, this.stock.Price));
                hasBuy = true;
            }
        }
    }


    #endregion

}
